package com.xhui.vimarket.zkplus.zul.attr.evt.rtime;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.metainfo.ComponentInfo;

import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.acom.xhui.vimarket.zkplusst;
import com.xhui.vimarket.zkcom.xhui.vimarket.zkplusean.EvtData;
import com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusan.impl.AttrTmplImpl;
import com.xhui.vcom.xhui.vimarket.zkpluszul.attr.evt.AttrBaseComposer;
com.xhui.vimarket.zkplus.vimarket.zkplus.zul.attr.mod.AttrComp;
com.xhui.vimarket.zkplus.vimarket.zkplus.zul.attr.mod.AttrCompCcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkplus.zul.attrcom.xhui.vimarket.zkplusctory;
import com.xhui.vimarket.zkplus.zul.acom.xhui.vimarket.zkpluspUtil;
import com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrMultiSubComcom.xhui.vimarket.zkplusxhui.vimarket.zkplus.zul.attr.msg.AttrMsgSupport;

/**
 * @版权：福富软件 版权所有 (c) 2010.
 * @文件：com.xhui.vimarket.zkplus.zul.attr.evt.AttrCheckboxComposer.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：控件事件处理
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public abstract class AttrRtimeBaseComposer extends AttrBaseComposer {
    
    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = 4579587521568091904L;
    /**
     * 日志. 
     */
    private static Logger     logger           = Logger.getLogger(AttrRtimeBaseComposer.class);
    
    @Override
    public ComponentInfo doBeforeCompose(final Page page, final Component comp,
        final ComponentInfo info) {
        return info;
    }
    
    @Override
    public void doAfterCompose(final Component comp) throws Exception {
        super.doAfterCompose(comp);
    }
    
    /**
     * getRootComp.
     * 
     * @return Component
     * @author wuyx
     * 2011-5-30 wuyx 
     */
    public abstract Component getRootComp();
    
    /**
     * onSelect.
     */
    public void onSetVal() {
        // EvtData
        final EvtData evtData = new EvtData();
        // attr 控件根
        final Component attrComp = this.getRootComp();
        // attr 顶级控件
        final AttrExt attrExt = AttrCompUtil.getAttrExtComp(attrComp);
        AttrComp attrCompAda = AttrCompFactory.getInstByComponent(attrComp);
        // 规则验证
        List<RetVO> valiRetVo = attrCompAda.validate(attrExt, attrComp);
        AttrMsgSupport.validateThrow(valiRetVo, attrComp);
        
        // 获取值,触发总控件事件
        logger.debug("fire onSetVal");
        // 如果本控件类型不是多实例，并且上级含有多实例控件，则需要触发多实例控件设置值
        final String thisMode = AttrCompUtil.getAttrCompMode(attrComp);
        final Component multiRootComp = AttrCompUtil.getSupCompRootByMode(attrComp,
            AttrTmplImpl.MODE_MULTISUB);
        if (multiRootComp != null && (!AttrTmplImpl.MODE_MULTISUB.equals(thisMode))) {
            // 触发值改变事件
            AttrMultiSubComp.fireChangeVal(multiRootComp);
        } else {
            // 否则直接触本控件设置值
            final ArrayList<AttrInst> retList = attrCompAda.getValue(attrExt, attrComp);
            if (retList == null || retList.size() < 1) {
                logger.debug("fire onSetVal remove");
                evtData.setAttrExt(attrExt);
                evtData.setAttrComp(attrComp);
                evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
                // 多实例特殊处理
                // 如果多实例，需要触发下级清空，暂时只能先支持下级一个情况
                if (AttrTmplImpl.MODE_MULTISUB.equals(thisMode)) {
                    List<Long> subAttrIds = AttrMultiSubComp.getSubCompAttrIds(attrComp);
                    if (subAttrIds.size() > 0) {
                        evtData.setAttrId(subAttrIds.get(0));
                    }
                }
                // 多实例处理End
                evtData.setOp(AttrCompConstants.OP_REMOVE);
                evtData.setDisplayMode(AttrCompUtil.getAttrCompMode(attrComp));
                attrExt.onSubChange(evtData);
            } else {
                logger.debug("fire onSetVal change");
                evtData.setAttrExt(attrExt);
                evtData.setAttrComp(attrComp);
                // 多实例处理
                if (retList.size() > 1) { // 多实例
                    evtData.setNewValList(retList);
                } else {
                    evtData.setNewVal(retList.get(0));
                }
                evtData.setAttrId(AttrCompUtil.getAttrCompAttrId(attrComp));
                evtData.setOp(AttrCompConstants.OP_ADD);
                evtData.setDisplayMode(AttrCompUtil.getAttrCompMode(attrComp));
                attrExt.onSubChange(evtData);
            }
        }
    }
}
